package com.chaoxin.lovehelp.manager;

import com.alibaba.dashscope.aigc.completion.ChatCompletion;
import org.springframework.stereotype.Component;
import com.alibaba.dashscope.app.*;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
/**
 * @author TengJay Huang
 * @version 1.0
 */
@Component
public class AiManager {
    private final String api_key="sk-ddc7e596cdb541e9904eb3a4c4ea191f";
    private final String app_id="f6f5eb362fce4fd7b29f84ccdfca33c4";

    //多轮对话
    public void requestAi(String userMessage,String sessionId) throws NoApiKeyException, InputRequiredException {
        ApplicationParam param = ApplicationParam.builder()
                // 若没有配置环境变量，可用百炼API Key将下行替换为：.apiKey("sk-xxx")。但不建议在生产环境中直接将API Key硬编码到代码中，以减少API Key泄露风险。
                .apiKey(api_key)
                // 替换为实际的应用 ID
                .appId(app_id)
                .prompt(userMessage)
                .sessionId("12d532a167be46d5ae96a68ce9a917d9")
                .build();

        Application application = new Application();
//        ApplicationResult result = application.call(param);
//        System.out.println(result.getOutput());
//        param.setSessionId(result.getOutput().getSessionId());
//        param.setPrompt("");
        ApplicationResult result = application.call(param);

        System.out.printf("%s\n, session_id: %s\n",
                result.getOutput().getText(), result.getOutput().getSessionId());
    };

    public static void main(String[] args) throws NoApiKeyException, InputRequiredException {
        AiManager aiManager = new AiManager();
        aiManager.requestAi("一个会员包含哪些信息","12d532a167be46d5ae96a68ce9a917d9");
    }
}
